<?php

/**
 * Class name    : LisaUtilities
 * Features      : Common function
 * Function list :  
 * Created Date  : 03/07/2008
 * Author        : Ha Quang Minh, Dang Huy Diep
 * Description   : 
 * 
 */

/**
 * Modified Date : 14/08/2008
 * Description   : Additional of the validate function. In order to avoid the SQL Injection
 * Author        : Ha Quang Minh
 * 
 * Modified Date : 19/08/2008
 * Description   : Changing in File Upload function, use for Prado
 * 
 * Modified Date : 16/12/2008
 * Description   : Add in the function that helps to compress a string
 * 
 */

class HUtil {
	function __construct() { }


	/**
	 * This function return the tag of the file
	 *
	 * @param $file the file whose tag to return
	 */
	function getTag($file){
		$tag=' ';
		$file=strtoupper($file);
		for($i=strlen($file)-1;$i>=0;$i--){
			if($file[$i]=='.') break;
		}
		$count=0;
		for($i=$i+1;$i<strlen($file);$i++){
			$tag[$count]=$file[$i];
			$count++;
		}
		if($tag!=' ') return $tag;
		else return null;
	}
	
	/**
	 * Upload a file to the server
	 *
	 * @param  $file is the array include file properties (name, tmp_name, size, etc.);
	 * @param  $dir	is the directory to upload file to
	 * @param $limitedExts allowed extensions to upload, this can be null (in that case, a @ sign should be placed before the command to avoid the notice message
	 * @return true if upload file successfully
	 */
	function uploadFile($filename, $filetemp, $dir, $limitedExts = null, $name = null){
		if($limitedExts == null || !isset($limitedExts) ) 
			$limitedExts = array("gif","jpg","jpeg","png","txt","nfo","doc","rtf","htm","dmg","zip","rar","gz","exe");
		if ($name == null) $name = 'match_'.date("YmdHis");
		$tag = self::getTag($filename);
		$newFile = $dir.'/'.$name.'.'.$tag;
		$return = $name.'.'.$tag;

		$pre = "<br><b>";
		$suf = " <img src=\"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/14.gif\">
				<img src=\"http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/45.gif\">
				<br>";
		
		if(isset($filename) == null){
			echo $pre."No file selected to upload!".$suf;
			return false;
		}
		if (!file_exists($filetemp)) {
			echo $pre.$file['name']." file does not exist!".$suf;
			return false;
		}
		/*if( !in_array(strtolower($tag), $limitedExts) ){
			echo $pre."Not allowed extension: ".$tag.$suf;
			return false;
		}*/
		if (!is_dir($dir)) {
			echo $pre.$dir." is not a directory!".$suf;
			return false;
		}
		if(!is_writable($dir)) {
			echo $pre.$dir." is not writable! Please chmod it first!".$suf;
			return false;
		}
		if (move_uploaded_file($filetemp, $newFile)) {
			return $return;
		}else {
			echo ($pre."Upload file ".$newFile." failed!".$suf);
			return false;
		}
	}
	
	/**
	 * This function will create a new editor
	 *
	 * @param varchar $FCKFolderPath the path of the folder fck editor
	 * @param number $width how wide is the editor window
	 * @param number $height how heigh is the editor window
	 * @param char $toolbarType Basic
	 * @param char $formName the name of the form (needed if you want to add more than one editor in one page)
	 * @param data $value values will be inserted into the editor when it is created
	 */
	function FCKEditor($FCKFolderPath, $value=null, $width=null, $height=null, $formName=null, $toolbarType=null){
		if($toolbarType == null) $toolbarType = "Default";
		if($width == null) $width = '100%';
		if($height == null) $height = '400';
		if($value == null) $value = '';
		if($formName == null) $formName = 'fckEditor';
		
		require_once($FCKFolderPath."/fckeditor.php");
		$sBasePath = $FCKFolderPath.'/' ;
		$oFCKeditor = new FCKeditor($formName) ;
		$oFCKeditor->BasePath	= $sBasePath ;
		$oFCKeditor->Height = $height;
		$oFCKeditor->Width  = $width;
		$oFCKeditor->ToolbarSet	= $toolbarType;
		$oFCKeditor->Value		= $value;
		return $oFCKeditor->Create();
	}

	/**
	 *  Function : redirect
	 *  Input    : $url
	 *  Output   : redirect to a page
	 */
	function redirect($url)
	{
		$i = 1;
		if ($i == 1)
		{
		?>
		<script language="javascript">
			window.location = "<?php echo $url; ?>";
		</script>
		<?php
		}
	}
	
	function goBack(){
		?><script>history.back();</script><?php
	}
	
	function alert($msg){
		 ?><script>alert('<?php echo $msg;?>');</script><?php 
	}
	
	/**
	 * This function will make the paging for every list
	 *
	 * @param $table Name of the table in the database
	 * @param $properties Properties of the query
	 * @param $limit Number of records per page
	 * @param $link Link of the module. Somekind of ?page=module
	 * @param $extra Link of the extra parameters. E.g: cid=4&action=4
	 */	
	function paging ($table, $properties = null, $limit, $link, $extra = null) {
		$query  = "SELECT * FROM ".$table." ".$properties;
		$result = @mysql_query($query) or die('Paging error!');
		$paging = (mysql_num_rows($result) / $limit) + 1 ;
		$result = "";
		for ($i = 1; $i < $paging; $i++) {
			$result = $result."<td><a href='".$link."&p=".$i."&".$extra."'>$i</a></td>\n";
		}
		return $result;
	}
	
	
	/**
	 * This function will make the paging for AJAX
	 *
	 * @param $table Name of the table in the database
	 * @param $properties Properties of the query
	 * @param $limit Number of records per page
	 * @param $link Link of the module. Somekind of ?page=module
	 * @param $id Id of the ajax loader
	 */	
	function paging_ajax ($table, $properties = null, $limit, $link, $id,$p = null) {
		$query  = "SELECT * FROM ".$table." ".$properties;
		$result = @mysql_query($query) or die('Paging error!');
		$paging = (mysql_num_rows($result) / $limit) + 1 ;
		$result = "";
		if ($p == null) $p = "p";
		for ($i = 1; $i < $paging; $i++) {
			$result = $result."<td><a href='#' onclick =\"load('$link"."&".$p."="."$i','$id');\">$i</a></td>";
		}
		return $result;
	}
	
	
	/**
	 * This function will validate the input value and return the fresh one
	 * 
	 * @param $input : the input value. Oftenly the text, the html content
	 * @return $output : validated input
	 */
	function validate($input) {
		$output = null;
		return $output;
	}
	
	/**
	 * Chuyển tiếng việt có dấu thành không dấu
	 *
	 * @param unknown_type $text
	 * @return unknown
	 */
	function convert2title($text) {
	
		$text = str_replace(
		array(' ','%',"/","\\",'"','?','<','>',"#","^","`","'","=","!",":" ,",,","..","*","&","__","▄"),
		array('_','' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'' ,'-','' ,'-','' ,'' ,'' , "_" ,"" ,""),
		$text);
		
		$chars = array("a","A","e","E","o","O","u","U","i","I","d", "D","y","Y");
		
		$uni[0] = array("á","à","ạ","ả","ã","â","ấ","ầ", "ậ","ẩ","ẫ","ă","ắ","ằ","ặ","ẳ","� �");
		$uni[1] = array("�?","À","Ạ","Ả","Ã","Â","Ấ","Ầ", "Ậ","Ẩ","Ẫ","Ă","Ắ","Ằ","Ặ","Ẳ","� �");
		$uni[2] = array("é","è","ẹ","ẻ","ẽ","ê","ế","�?" ,"ệ","ể","ễ");
		$uni[3] = array("É","È","Ẹ","Ẻ","Ẽ","�?","Ế","Ề" ,"Ệ","Ể","Ễ");
		$uni[4] = array("ó","ò","�?","�?","õ","ô","ố","ồ", "ộ","ổ","ỗ","ơ","ớ","�?","ợ","ở","� �");
		$uni[5] = array("Ó","Ò","Ọ","�?","Õ","Ô","�?","Ồ", "Ộ","Ổ","Ỗ","Ơ","�?","Ờ","Ợ","�?","� �");
		$uni[6] = array("ú","ù","ụ","ủ","ũ","ư","ứ","ừ", "ự","ử","ữ");
		$uni[7] = array("�?","Ù","Ụ","Ủ","Ũ","Ư","Ứ","Ừ", "Ự","Ử","Ữ");
		$uni[8] = array("í","ì","ị","ỉ","ĩ");
		$uni[9] = array("�?","Ì","�?","Ỉ","Ĩ");
		$uni[10] = array("đ");
		$uni[11] = array("�?");
		$uni[12] = array("ý","ỳ","ỵ","ỷ","ỹ");
		$uni[13] = array("�?","Ỳ","Ỵ","Ỷ","Ỹ");
		
		for($i=0; $i<=13; $i++) {
		$text = str_replace($uni[$i],$chars[$i],$text);
		}
		
		return $text;
	}
	
	/**
	 * Lisa Compress
	 * 
	 * Compress a string
	 *
	 * @param string $string
	 * @return a string
	 */
	function Compress($string) {
		return bzcompress($string);
	}
	
	/**
	 * Lisa Decompress
	 * 
	 * Decompress a string
	 *
	 * @param string $string
	 * @return a string
	 */
	function Decompress($string) {
		return bzdecompress($string);
	}	
	
	
} // END OF CLASS

?>